<?php

// 本类由系统自动生成，仅供测试用途
/**
 * PublicAction
 * 
 * @package 
 * @author www.80boys.com
 * @copyright 2012
 * @version $Id$
 * @access public
 */
class PublicAction extends Action
{
    protected $system = array();
    protected function _initialize()
    {
        $this->system = $this->getConfig();
    }
    public function login()
    {
        $this->assign('system', $this->system);
        $this->display();
    }
    public function verify()
    {
        import("ORG.Util.Image");
        Image::buildImageVerify();
    }
    public function main()
    {
        $this->assign('self', session('_LOGIN_'));
        $this->display();
    }
    public function loginOut()
    {
        D('User')->loginOut($_POST);
        $this->redirect('Public/login', array(), 1, '注销成功...');
    }
    public function doLogin()
    {
        $system = $this->system;
        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        // 数据检查
        if($_SESSION['verify'] != md5($_POST['verify']) && $system['webverify'])
        {
            $this->error('验证码错误', U('Public/login'));
            exit();
        }
        if(empty($_POST['username']) || empty($_POST['password']))
        {
            $this->error('用户名或密码不能为空', U('Public/login'));
            exit();
        }
        if(false == D('User')->signin($data))
        {
            $this->error('用户名或密码错误', U('Public/login'));
            exit();
        } else
        {
            $this->success('登录成功!', U('Index/index'));
            exit();
        }
    }
    /*
    * 管理员登出
    * 
    */
    public function logout()
    {
        D('User')->logout();
        $this->success('注销成功!', U('Public/login'));
    }
    /*
    * 用户修改资料
    * 
    */
    public function editAdmin()
    {
        if($_POST['id'])
        {
            $data['id'] = $this->_post('id');
            $data['password'] = md5($this->_post('newpassword'));
            M('User')->save($data);
            D('User')->logout();
            $this->alert('密码修改成功', 'login');
            exit();
        }
        $this->assign('admin', session('_LOGIN_'));
        $this->display();
    }
    public function checkPassword()
    {
        $admin = session('_LOGIN_');
        $pass = md5(trim($_POST['password']));
        echo ($pass == $admin['password']) ? '1' : '0';
    }
    private function getConfig()
    {
        $row = M('Setting')->where(" style = 'system' ")->field('*')->find();
        return json_decode($row['value'], true);
    }
    /**
     * 弹出框
     * @param $str
     * @param $url
     */
    public function alert($str, $url = '')
    {
        $go_url = empty($url) ? 'history.go(-1)' : "parent.location.href='{$url}'";
        $alert = <<< EOF
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>提示信息</title>
		<script language='javascript' type='text/javascript' charset='utf-8'>alert('{$str}');{$go_url};</script>
		</head>
		<body>
		</body>
		</html> 
EOF;
        die($alert);
    }
}
